import {
  app,
  protocol,
  BrowserWindow,
  Tray,
  Menu,
  shell,
  dialog,
  ipcMain
} from "electron";
import { createProtocol } from "vue-cli-plugin-electron-builder/lib";

import path from "path"

const isDevelopment = process.env.NODE_ENV !== "production";
let tray = null;
let win = null;
let quitWin = null;
let preferencesWin = null;
let datelist = null;

protocol.registerSchemesAsPrivileged([
  {
    scheme: "app",
    privileges: {
      secure: true,
      standard: true,
    },
  },
]);

async function createWindow() {
  //主窗口
  win = new BrowserWindow({
    width: 1000,
    height: 650,
    frame: false,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      webSecurity: false
    },
  });

  //退出窗口
  quitWin = new BrowserWindow({
    width: 400,
    height: 300,
    frame: false,
    show: false,
    resizable: false,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      webSecurity: false
    },
  });

  // 配置窗口
  preferencesWin = new BrowserWindow({
    width: 600,
    height: 400,
    frame: false,
    show: false,
    resizable: false,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      webSecurity: false,
      allowRunningInsecureContent: false,
    },
  });

  // 数据列表窗口
  datelist = new BrowserWindow({
    width: 800,
    height: 600,
    frame: false,
    show: false,
    resizable: false,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      webSecurity: false,
      allowRunningInsecureContent: false,
    },
  });

  if (process.env.WEBPACK_DEV_SERVER_URL) {
    await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL);
    await quitWin.loadURL(process.env.WEBPACK_DEV_SERVER_URL + "#quit");
    await preferencesWin.loadURL(
      process.env.WEBPACK_DEV_SERVER_URL + "#preferences"
    );
    await datelist.loadURL(process.env.WEBPACK_DEV_SERVER_URL + "#/datalist");
    if (!process.env.IS_TEST) {
      win.webContents.openDevTools();
      quitWin.webContents.openDevTools();
      preferencesWin.webContents.openDevTools();
      datelist.webContents.openDevTools();
    }
  } else {
    createProtocol("app");
    win.loadURL("app://./index.html");
    quitWin.loadURL("app://./index.html#quit");
    preferencesWin.loadURL("app://./index.html#preferences");
    datelist.loadURL("app://./index.html#datalist");
  }

  tray = new Tray(path.join(path.join(__static, 'favicon.ico')));
  const trayMenu = Menu.buildFromTemplate([
    {
      label: "显示主界面",
      click: () => {
        win.restore();
        win.show();
      },
    },
    {
      label: "关于软件",
      click: () => {
        dialog.showMessageBox({
          type: "info",
          title: "关于软件",
          message:
            "遥感数据检索器(山东高分版), GeoTech_QingQian`sDaddy编写。\n 日照极泰空间信息技术有限公司 出品",
          buttons: ["确定"],
        });
      },
    },
    {
      label: "偏好设置",
      click: () => {
        win.show();
        preferencesWin.show();
      },
    },
    {
      label: "教程帮助",
      click: () => {
        shell.openExternal(
          "https://www.yuque.com/geotechqingqiansdaddy/kzi4vg"
        );
      },
    },
    {
      label: "退出",
      click: () => {
        app.exit();
      },
    },
  ]);

  tray.on("right-click", () => {
    tray.popUpContextMenu(trayMenu);
  });

  tray.on("double-click", () => {
    win.show();
  });
  tray.setToolTip("高分数据检索器");
  tray.setContextMenu(trayMenu);
}

app.on("ready", async () => {
  createWindow();
});
app.on("window-all-closed", () => {
  app.quit();
});

if (isDevelopment) {
  if (process.platform === "win32") {
    process.on("message", (data) => {
      if (data === "graceful-exit") {
        app.quit();
      }
    });
  } else {
    process.on("SIGTERM", () => {
      app.quit();
    });
  }
}

// ipcMain监控最小化、最大化、关闭主窗口
ipcMain.on("change_mainWindow", (evt, arg) => {
  if (arg == "minimum") {
    win.minimize();
  } else if (arg == "maximum") {
    win.isMaximized() ? win.restore() : win.maximize();
  } else if (arg == "close_quit") {
    app.exit();
  } else if (arg == "close_hide") {
    win.hide();
  } else if (arg == "close_null") {
    quitWin.show();
  }
});

// ipcMain 退出提示窗的关闭
ipcMain.on("change_quitWindow", () => {
  quitWin.hide();
});

ipcMain.on("change_preferencesWindow", () => {
  preferencesWin.hide();
});

ipcMain.on("createDirSelectDialog", (evt) => {
  const dirPath = dialog.showOpenDialogSync({
    title: "请选择输出位置",
    properties: ["openDirectory"],
  });

  if (dirPath) {
    evt.returnValue = dirPath[0];
  } else {
    evt.returnValue = "null";
  }
});

ipcMain.on("close_datalistWindow", () => {
  datelist.hide();
});
ipcMain.on("open_datalistWindow", () => {
  datelist.show();
});

ipcMain.on("sendMsg", (e, msg) => {
  datelist.webContents.send("snedResult", msg);
});

ipcMain.on("sendBox", (e, msg) => {
  win.webContents.send("sendBoxtoMap", msg);
});

ipcMain.on("loadImg", (e, msg) => {
  win.webContents.send("sendImgtoMap", msg);
});

ipcMain.on("drawing", () => {
  win.webContents.send("draw");
});
